<?php
// $Id: table_waimai.php 434 2012-06-06 00:32:17Z tomqin@gmail.com $

class Table_Waimai_Module{
    private $user = null;

    public function __construct($user){
        $this->user = $user;
    }

    public function check_user_right($shop_id){
        if($this->user->status == USER_ADMIN){
            return true;
        }elseif($this->user->status == USER_BUSINESS){
            $uid = $this->user->uid;
            $shop_id = intval($shop_id);
            return Db::getFirst("SELECT count(shop_id) FROM `biz_shop` WHERE admin_uid = '$uid' AND shop_id = '$shop_id'");
        }else{
            return false;
        }
    }

    public function get_data($shop_id, $sample = false){
        $shop_id = intval($shop_id);
        $sql  = "SELECT * FROM `biz_waimai` WHERE shop_id = '{$shop_id}'";
        return Db::getRow($sql);
    }

    public function update($shop_id, array $post){
        $post['shop_id'] = intval($shop_id);
        $data = $this->waimai_fields($post);
        Db::insert('biz_waimai', $data, true);
        $list_data = array(
            'delivery_time' => $data['delivery_time'],
            'delivery_desc' => $data['delivery_desc'],
        );
        Db::update('biz_shop', array('list_data' => serialize($list_data)), array('shop_id'=> $shop_id));
    }

    protected function waimai_fields($post){
        return array(
            'shop_id'       => array_get_int($post, 'shop_id'),
            'delivery_time' => array_get_string($post, 'delivery_time'),
            'delivery_desc' => array_get_string($post, 'delivery_desc'),
            'min_charge'    => array_get_int($post, 'min_charge'),
            'min_order'     => array_get_int($post, 'min_order'),
        );
    }

    //修改附属表
    public function replace_deliver($shop_id, array $post){
        $shop_id = intval($shop_id);
        Db::query("DELETE FROM `biz_waimai_deliver` WHERE `shop_id` = '{$shop_id}'");
        if(isset($post['city_deliver']) && $post['city_deliver']){ //全市可送
            $data = array(
                'shop_id'  => $shop_id,
                'zone_id' => 0,
                'all_zone'=> 1,
            );
            Db::insert('biz_waimai_deliver', $data);
         }else{
            $deliver_zone = array_get($post, 'deliver_zone');
            $zone_all = array_get($post, 'zone_all');
            $parent_zone  = $array = array();
            if($zone_all || $deliver_zone){
                $zone = App::module('zone', '', array(App::getCityId()));

                //判断区域
                if(is_array($zone_all)){
                    foreach($zone_all as $zid){
                        $parent_zone[$zid] = 1;
                    }
                }

                //判断二级版块
                if(is_array($deliver_zone)){
                    foreach($deliver_zone as $dzone){
                        $pid = $zone->getZoneById($dzone)->parent_id;
                        if(isset($parent_zone[$pid]) && $parent_zone[$pid]){ //如果已经是“全区”，就不必处理
                            continue;
                        }
                        $array[] = array(
                            $shop_id, $dzone, 0
                        );
                        $parent_zone[$pid] = 0;
                    }
                }

                //处理区域
                foreach($parent_zone as $zone_id => $all_zone){
                    $array[] = array(
                        $shop_id, $zone_id, $all_zone,
                    );
                }
                $stmt = DB::prepare('INSERT INTO `biz_waimai_deliver` (shop_id, zone_id, all_zone) VALUES (?, ?, ?)');
                $stmt->bind_param('iii', $shop_id, $zone_id, $all_zone);
                foreach($array as $data){
                    list($shop_id, $zone_id, $all_zone) = $data;
                    $stmt->execute();
                }
            }
        }
    }
}

?>